package com.eastfair.auth.service.impl;

//import com.baomidou.dynamic.datasource.annotation.DS;
import com.eastfair.auth.dto.OrganizationDTO;
import com.eastfair.auth.entity.Organization;
import com.eastfair.auth.enumeration.OrganizationOrgTypeEnum;
import com.eastfair.auth.enumeration.OrganizationVestTypeEnum;
import com.eastfair.auth.service.*;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.exception.code.ExceptionCode;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 场馆内部操作业务接口实现类
 *
 * </p>
 *
 * @author ligang
 * @date 2022-07-16
 */
@Slf4j
@Service
//@DS("#thread.tenant")
public class XiamenOrganizationServiceImpl implements XiamenOrganizationService {

    @Autowired
    OrganizationService organizationService;

    @Autowired
    ResourceOrganizationService resourceOrganizationService;

    @Autowired
    UserOrganizationService userOrganizationService;

    @Autowired
    OrganizationRoleService organizationRoleService;

    @Override
    public Organization query(OrganizationDTO organizationDTO, OrganizationOrgTypeEnum resourceOrgType) {
        Organization organization = organizationService.queryOrgOfTypeAndResource(resourceOrgType, organizationDTO.getResourceId(), null);
        return organization;
    }

    @Override
    public R<Organization> create(OrganizationDTO organizationDTO, OrganizationOrgTypeEnum resourceOrgType, OrganizationOrgTypeEnum resourceOrgTypeStaff) {
        Organization organization = query(organizationDTO, resourceOrgType);
        if (organization == null) {
            organizationDTO.setOrgType(resourceOrgType);
            organizationDTO.setVestType(OrganizationVestTypeEnum.R);
            //创建并通过userid设置管理员
            R<Organization> r = organizationService.createOrg(organizationDTO);
            Organization bookingOrganization = r.getData();
            //创建旗下员工组
            OrganizationDTO organizationStaff = new OrganizationDTO();
            organizationStaff.setParentId(bookingOrganization.getId());
            organizationStaff.setName(resourceOrgTypeStaff.getDesc());
            organizationStaff.setOrgType(resourceOrgTypeStaff);
            organizationStaff.setVestType(OrganizationVestTypeEnum.R);
            return organizationService.createOrg(organizationStaff);
        } else {
            organizationDTO.setOrgId(organization.getId());
            return upd(organizationDTO);
        }
    }

    @Override
    public R<Organization> upd(OrganizationDTO organizationDTO) {
        return organizationService.updOrg(organizationDTO);
    }

    @Override
    public R del(OrganizationDTO organizationDTO, OrganizationOrgTypeEnum resourceOrgType) {
        Organization organization = organizationService.queryOrgOfTypeAndResource(resourceOrgType, organizationDTO.getResourceId(), null);
        if (organization == null) {
            return R.success();
        }
        return organizationService.del(organization.getId());
    }

    @Override
    public R addUser(OrganizationDTO organizationDTO, Integer isAdmin, OrganizationOrgTypeEnum resourceOrgType, OrganizationOrgTypeEnum resourceOrgTypeStaff) {
        Long orgId = null;
        if (organizationDTO.getOrgId() != null) {
            orgId = organizationDTO.getOrgId();
        } else {
            //找到属于资源的节点
            Organization organization = query(organizationDTO, resourceOrgType);
            if (organization == null) {
                return R.fail(ExceptionCode.ORG_NULL);
            }

            if (isAdmin == BusinessConstant.YES) {
                //添加管理员
                organizationDTO.setOrgType(resourceOrgType);
            } else {
                //添加普通员工
                organizationDTO.setOrgType(resourceOrgTypeStaff);
            }
            List<Organization> organizationList = organizationService.findResource(organizationDTO.getOrgType(), null, organization.getId(), organizationDTO.getResourceId());
            if (organizationList == null || organizationList.isEmpty()) {
                return R.fail(ExceptionCode.ORG_NULL);
            }
            orgId = organizationList.get(0).getId();
        }
        //添加用户
//        organizationService.addUseridOfOrg(orgId, organizationDTO.getUserIds());
        return R.success();
    }

    @Override
    public R breakUser(OrganizationDTO organizationDTO, OrganizationOrgTypeEnum resourceOrgType) {
        if (organizationDTO.getOrgId() != null) {
            //解散用户
            organizationService.breakUseridOfOrg(organizationDTO.getOrgId(), organizationDTO.getUserIds());
        } else {
            //找到属于资源的节点
            Organization organization = query(organizationDTO, resourceOrgType);
            if (organization == null) {
                return R.fail(ExceptionCode.ORG_NULL);
            }
            //查询全部子集节点
            OrganizationDTO organizationDTO2 = new OrganizationDTO();
            organizationDTO2.setCode(organization.getCode());
            List<Organization> organizationList = organizationService.queryAllOfCode(organizationDTO2);
            if (organizationList == null || organizationList.isEmpty()) {
                return R.fail(ExceptionCode.ORG_NULL);
            }
            for (Organization organization1 : organizationList) {
                //解散用户
                organizationService.breakUseridOfOrg(organization1.getId(), organizationDTO.getUserIds());
            }
        }

        return R.success();
    }

}
